<template>
	<view>
		<uni-forms ref="form" :modelValue="formData" :rules="rules">
			<uni-forms-item label="用户名" name="name">
				<uni-easyinput type="text" v-model="formData.name" placeholder="请输入用户名" />
			</uni-forms-item>
			<uni-forms-item label="密码" name="password">
				<uni-easyinput type="password" v-model="formData.password" placeholder="请输入密码" />
			</uni-forms-item>
		</uni-forms>
		<button @click="submit">登录</button>
		<navigator url="/pages/reg/reg"><button >注册</button></navigator>
		<navigator url="/pages/findpd/findpd"><text>忘记密码？</text></navigator>
	</view>
</template>

<script>
		import {
			login,
		} from "@/api/client.js";
	export default {
		data() {
			return {
				// form data
				formData: {
					name: '',
					password: ''
				},
				rules: {
					// Required validation for the name field
					name: {
						rules: [{
								required: true,
								errorMessage: '请输入用户名',
							},
							{
								minLength: 2,
								maxLength: 5,
								errorMessage: '姓名长度在 {minLength} 到 {maxLength} 个字符',
							}
						]
					},
					// Required validation for email field
					password: {
						rules: [{
							format: 'password',
							errorMessage: '密码错误',
						}]
					}
				}
			}
		},
		methods: {
      /**
       * 复写 binddata 方法，如果只是为了校验，无复杂自定义操作，可忽略此方法
       * @param {String} name 字段名称
       * @param {String} value 表单域的值
       */
      // binddata(name,value){
      //   this.$refs.form.setValue(name, value)
      // },
      // trigger submit form
      async submit() {
        this.$refs.form.validate().then(res => {
          console.log('表单数据信息：', res1);
        }).catch(err => {
          console.log('表单错误信息：', err);
        })
        let res1 = await login(this.formData)
        if (res1.state == 200) {
          uni.setStorageSync("token", res1.data);
		  uni.switchTab({
		  						url:'/pages/index/index'
		  					});

          console.log('表单数据信息：', res1);

        }
      }
    }}
	

</script>

<style>

</style>
